/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.machine.spectrum.ula;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.In;
import com.dustedpixels.cirt.model.core.Out;
import com.dustedpixels.cirt.model.core.Pins;
import com.dustedpixels.cirt.model.core.Script;

/**
 * Decodes color index from: screen bit, attr and flashReverse.
 * 
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class ColorDecoder {
  // true = ink, false = paper
  @In public boolean ink;
  @In public boolean bright;
  @In public boolean flash;
  
  @In @Pins(3) public int paperColor;
  @In @Pins(3) public int inkColor;
  
  // true = reverse ink and paper when flash is active, false = no reverse
  @In public boolean flashReverse;
  
  // output color: bit 3 = bright, bits 210 = RBG (or something)
  @Out @Pins(4) public int color;
  
  @Script
  public void decode() {
    boolean actualInk = ink ^ (flashReverse & flash);
    
    if (actualInk) {
      color = inkColor;
    } else {
      color = paperColor;
    }
    
    if (bright) {
      color |= 0x08; 
    }
  }
}
